<?php
	class MasterController
	{
		// Membervariables
		private $_con = null;
		
		public function __construct(\DBConnection $con)
		{
			$this->_con = $con;
		}
		
		public function DoControl()
		{
			$body = "";
			// UserView/Handler
			$userView = new UserView();
			$userHandler = new UserHandler($this->_con, $userView);
			// Handlers.
			$loginHandler = new LoginHandler($userHandler);
			$registerHandler = new RegisterHandler($userHandler);
			// Controllers.
			$loginController = new LoginController($loginHandler);
			$registerController = new RegisterController($registerHandler);

			// Views.
			$registerView = new RegisterView();
			$navigationView = new NavigationView();
			// MessageView/Handler
			$messView = new MessageView($userHandler, $navigationView);
			$messageHandler = new MessageHandler($this->_con, $messView, $userHandler);
			$messageController = new MessageController($userHandler, $messView, $messageHandler);	
			// ProfileView/Handler/Controller
			$profileView = new ProfileView();
			$profileHandler = new ProfileHandler($this->_con);
			$profileController = new ProfileController($userHandler, $profileView, $profileHandler);
			
			$userIsLoggedInController = new UserIsLoggedInController($userHandler, $registerHandler, $profileHandler, $profileView, $profileController, $messageHandler, $messView, $messageController, $registerView, $userView);

			$body .= $loginController->DoControl();
			
			// Check if the user is logged in.
			if ($loginHandler->IsLoggedIn() == false)
			{
				// Check if The user tried to register a new account.
				if ($registerView->TriedToRegister() == true || $navigationView->UserPressedRegister() == true)
				{
					$body .= $registerController->DoControl();
					$body .= $navigationView->GetWantToGoBackFromRegisterLink();
				}
				else
				{
					$body .= $navigationView->GetWantToRegisterLink();
				}
				$body .= "<div class ='MainFrame'>";
			}
			else
			{
				$body .= "<div class ='MainFrame'>";
				$body .= $userIsLoggedInController->DoControl();
			}
			
			return $body;
		}		
	}		